/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import at.socialconference.app.model.ATalk;
import at.socialconference.app.twitter.TwitterHelper;
import at.socialconference.inter.model.User;

public class TalkActivity extends Activity implements OnClickListener {

	private TextView tvTitle;
	private TextView tvDescription;
	private TextView tvFromTo;
	private ImageButton btnTweet;

	private ATalk talk;
	private EditText et_input;

	public String format(Date d, SimpleDateFormat sdf){
		return d!=null?sdf.format(d):"";
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.talkview);

		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvDescription = (TextView) findViewById(R.id.tv_description);
		tvFromTo = (TextView) findViewById(R.id.tv_fromto);
		btnTweet = (ImageButton) findViewById(R.id.tv_btnTweet);
		btnTweet.setOnClickListener(this);
		Bundle b = getIntent().getExtras();
		talk = (ATalk)b.getParcelable("talk");

		if(talk != null){
			tvTitle.setText(talk.getTitle()!=null?talk.getTitle():"--");
			tvDescription.setText(talk.getDescription()!=null?talk.getDescription():"--");

			SimpleDateFormat dform = new SimpleDateFormat("dd.MM");
			SimpleDateFormat tform = new SimpleDateFormat("HH.mm");
			String tmp = format(talk.getDate(),dform);
			tmp += " ";
			tmp += format(talk.getFrom(),tform);
			tmp += " - ";
			tmp += format(talk.getTo(),tform);

			tvFromTo.setText(tmp);
		}
	}

	@Override
	public void onClick(View v) {

		AlertDialog.Builder inputDialog;
		inputDialog = new AlertDialog.Builder(this);
		inputDialog.setTitle("Enter Tweet");
		et_input = new EditText(this);
		et_input.setWidth(250);
		et_input.setHeight(60);
		et_input.setLines(3);
		et_input.setText(createTweet());
		inputDialog.setView(et_input);
		inputDialog.setPositiveButton("Ok",
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				String txt_tweet= et_input.getText().toString();

				TwitterHelper.tweet(txt_tweet);
			}
		});
		inputDialog.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		});
		inputDialog.show();
	}

	private CharSequence createTweet() {
		User u =  talk.getaSpeakers()!=null&&talk.getaSpeakers().length>0?talk.getaSpeakers()[0]:null;
		String tmp = "Enjoying \""+talk.getTitle()+"\"";
		if(u!=null){
			String speakerstr =" by "+u.getFullName();
			if(tmp.length()+speakerstr.length()<161)
				tmp += speakerstr;
		}
		return tmp;
	}
}
